package com.thiege.mall.controller.protal;

import com.thiege.mall.common.constant.ProjectConst;
import com.thiege.mall.common.constant.RespMessage;
import com.thiege.mall.common.dto.ServerResponse;
import com.thiege.mall.domain.User;
import com.thiege.mall.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 用户请求控制器
 * Created by wang on 2017/5/31.
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    private final IUserService iUserService;

    @Autowired
    public UserController(IUserService iUserService) {
        this.iUserService = iUserService;
    }

    /**
     * 用户登录
     * @param username 用户名
     * @param password 密码
     * @param session session
     * @return 登录结果
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse userLogin(String username, String password, HttpSession session) {
        //验证参数
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return ServerResponse.newInstanceOfFailure(RespMessage.common.PARAM_ILLEGAL);  //非法参数
        }
        ServerResponse serverResponse = iUserService.userLogin(username, password);
        //判断是否登录成功
        if (serverResponse.isSuccess())
            session.setAttribute(ProjectConst.common.CURRENT_USER, serverResponse.getData());
        return serverResponse;
    }

    @RequestMapping(value = "/userInfo",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse userRegiste(User user,) {

    }
}
